import "../../../../scss/activity/collect/friends_help_edit.scss"

import $ from "jquery";
import Vue from "vue";
import moment from "moment";
import "../../../utils/weui";
import com from "../../../utils/common";
import autosize from "../../../utils/autosize";
import autoplay from "../../components/autoplaymusic";
import EditSection from "../../components/section";
import FuncBanner from "../../components/funcbanner";
import EditHeader from "../../components/editHeader";
import EditTime from "../../components/editTime";
import EditAddMsg from "../../components/editAddFooter";
import defData from "./friends_data";


$(function () {

  let page = {
    _actid: com.getUrlParam("actid") ? com.getUrlParam("actid") : 1,
    _app:{},
    _actData:defData.edit,
    _init:function () {
      let self = this;
      com.ajax({
        url: "/activity/getActDetial",
        data: {Actid: self._actid},
        ok: function (data) {
          console.log(data["msg"]);
          let temp = data["msg"];
          if (temp !== "fail" && typeof temp === "object") {//如果没有取到值，则使用默认值，用户创建新活动时
            let tempData = {};
            tempData.Actid = self._actid;
            tempData.Prizes = temp.Prizes ? JSON.parse(temp.Prizes) : temp.Prizes;
            tempData.Content = JSON.parse(temp.Content);
            tempData.Header = {};
            // 存储标题音乐,以及头图
            tempData.Header.Title = temp.Title;
            tempData.Header.BgMusic = temp.BgMusic;
            tempData.Header.BannerStr = temp.BannerStr;
            // 存储时间
            tempData.TimeSet = {};
            tempData.TimeSet.StartTime = moment(temp.StartTime).format("YYYY-MM-DD HH:mm");
            tempData.TimeSet.EndTime = moment(temp.EndTime).format("YYYY-MM-DD HH:mm");
            // 存储额外信息(报名,电话等)
            tempData.AddMsg = {};
            tempData.AddMsg.Other = JSON.parse(data["msg"]["Other"]);
            tempData.AddMsg.Mobile = temp.Mobile;

            // 存储分享信息
            tempData.ShareContent = temp.ShareContent;
            tempData.ShareImg = temp.ShareImg;
            $.extend(self._actData, tempData);
            self._loadTemp();
          }else{
            self._loadTemp();
          }
          autoplay();
        }
      })

    },
    _loadTemp:function () {
      let self = this;

      Vue.component("g-section", EditSection)
      Vue.component("func-banner", FuncBanner);
      Vue.component("edit-header", EditHeader);
      Vue.component("select-time",EditTime);
      Vue.component("edit-addmsg",EditAddMsg);
      self._app = new Vue({
        el: "#app",
        data: self._actData,
        methods: {
          checkbox: function (item) {
            item = !item;
          },
          listenSectionChange: function (val) {
            // this.Section = val["Section"];
          },
          //保存&预览活动
          saveActDetail: function (sta) {
            console.log(this);
            let self = this.$data;
            let parse = {};
            parse.Actid = self.Actid;
            parse.Catgory = "collect/friends_help";
            parse.StartTime = $("#start_time").val();
            parse.EndTime = $("#end_time").val();
            parse.Title = self.Header.Title;
            parse.BgMusic = self.Header.BgMusic;
            parse.BannerStr = self.Header.BannerStr;
            parse.Mobile = self.AddMsg.Mobile;
            parse.PrizesStr = JSON.stringify(self.Prizes);
            parse.OtherStr = JSON.stringify(self.AddMsg.Other);
            parse.SectionStr = JSON.stringify(self.Content)
            parse.Status = sta;
            parse.ShareImg = self.ShareImg;
            parse.ShareContent = self.ShareContent;
            if (moment(parse.EndTime).isBefore(parse.StartTime)) {
              com.alertDialog("开始时间应该小于结束时间");
              return
            }
            console.log(parse);
            com.ajax({
              url: "/activity/saveCollectComeOn",
              data: parse,
              method: "post",
              ok: function (data) {
                console.log(data);
                if (data["msg"] == "ok") {
                  let ActId = data["Actid"]
                  console.log(ActId);
                  com.savedb(ActId,true);
                  let url = "";
                  if(sta == 1){
                    url = "/dist/activity/collect/friends_help.html?actid=" + data["Actid"] + "&preview=1"
                  }else{
                    url = "/dist/activity/collect/friends_help.html?actid=" + data["Actid"]
                  }
                  let editUrl = "/dist/activity/collect/friends_help_edit.html?actid=" + data["Actid"]
                  com.saveSuc(url,editUrl)
                  // 让用户可以在这个页面刷新一次
                  $(".layui-m-layershade").click(function () {
                    window.location = editUrl
                  })
                }
              }
            });
          }
        },
        filters: {
          moment(date) {
            let d = moment(date);
            if (!d.isValid(date)) return null;
            return d.format("YYYY-MM-DD HH:mm");
          }
        },
        updated: function () {
          console.log("更新");
        }
      })

      self._afterTemp();
    },
    _afterTemp: function () {
      // 需要加载在模板渲染之后
      autosize(document.querySelectorAll("textarea"));
      this.closeDailogEnable(); //注册事件
    },
    //页面统一弹框关闭
    closeDailogEnable: function () {
      $("#app").on("click", ".cross ,.makesure", function (e) {
        console.log(e)
        $(this)
          .parent()
          .parent()
          .hide();
        $(".mask").hide();
        $(".management").show();
      });
    }
  }


  page._init()
})